import utils.DBUtil as db
import numpy as np
import matplotlib.pyplot as plt

conn = db.get_conn()
cursor = conn.cursor()
sql = "select d_date,up_down,final_price,turnover_rate from his_data WHERE d_code = '东方环宇' and d_date >= '2023-10-01' ORDER BY d_date asc"

cursor.execute(sql)
result = cursor.fetchall()

# x轴信息
x = range(0, len(result))

# plt.figure(figsize=(10, 6))
data = np.array(result)
d_date = data[:, 0]

up_down = data[:, 1]
up_down = up_down / np.max(up_down)

final_price = data[:, 2]
final_price = final_price / np.max(final_price)

turnover_rate = data[:, 3]
turnover_rate = turnover_rate / np.max(turnover_rate)

plt.figure(figsize=(12, 8))
# 2.绘制图像  '-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted'
plt.plot(x, up_down, color="r", linestyle="dashdot", label="up_down")
# plt.plot(x, final_price, color="g", linestyle="dashdot", label="final_price")
plt.plot(x, turnover_rate, color="b", linestyle="dashdot", label="turnover_rate")
x_ticks_label = ["{}".format(i) for i in x]
y_ticks = range(1)
# 刻度显示
plt.xticks(x[::1], x_ticks_label[::1])
plt.yticks(y_ticks[::1])
plt.grid(True, linestyle="-", alpha=1)

plt.title("stock", fontsize=20)

# # 创建绘图对象
# fig, ax = plt.subplots()
#
# # 添加折线图
# ax.plot(x, up_down, label='涨幅')
# ax.plot(x, final_price, label='final_price')
# ax.plot(x, turnover_rate, label='换手率')

plt.show()

# 关闭游标和连接
cursor.close()
conn.close()
